﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;

namespace SharpSoft.IO.Ports
{
    /// <summary>
    /// 转速传感器
    /// </summary>
    public class SpeedSensor : IDisposable
    {
        public SpeedSensor(string port)
        {
            mod = new modbus();
            mod.Open(port, 9600);
            isRun = true;
            thread = new Thread(threadWhile);
            thread.Start();
        }

        private bool isRun = false;

        private Thread thread;
        modbus mod;
        private void threadWhile()
        {
            while (isRun)
            {
                int value = 0;
                switch (this.UsedDevice)
                {
                    case DeviceType.ZNZS6E1R:
                        value = ZNZS6EI1R_GetValue();
                        break;
                    case DeviceType.HB965SF:
                        value = HB965SF_GetValue();
                        break;
                    default:
                        break;
                }
                Thread.Sleep(this.Interval);
                this.OnGotSpeed(value);
            }
        }
        /// <summary>
        /// 间隔时间
        /// </summary>
        public int Interval { get; set; } = 100;
        /// <summary>
        /// 使用的设备类型
        /// </summary>
        public DeviceType UsedDevice { get; set; } = DeviceType.ZNZS6E1R;
        private void OnGotSpeed(int speed)
        {

            GotValue?.Invoke(speed);
        }

        private int ZNZS6EI1R_GetValue()
        {
            short[] values = new short[2];
            while (!mod.SendFc3(1, 0, 2, ref values) && isRun)
            {
                Thread.Sleep(100);
            }
            var low = BitConverter.GetBytes(values[0]);
            var hig = BitConverter.GetBytes(values[1]);
            List<byte> list = new List<byte>(8);
            list.AddRange(low);
            list.AddRange(hig);
            var r = BitConverter.ToInt32(list.ToArray());
            return r;
        }

        /// <summary>
        ///  HB965SF测距传感器获取值
        /// </summary>
        /// <returns></returns>
        private int HB965SF_GetValue()
        {
            short[] values = new short[2];
            while (!mod.SendFc3(1, 4, 2, ref values) && isRun)
            {
                Thread.Sleep(100);
            }

            var low = BitConverter.GetBytes(values[0]);
            var hig = BitConverter.GetBytes(values[1]);

            //参数数据共有两种类型：
            //  二字节数据和四字节数据。其中四字节数据前三字节表示参数值（补码形式,最高位为符号位），后一
            //  字节表示小数点位置。

            List<byte> list = new List<byte>(4) { hig[1], low[0], low[1], ((low[1] & 128) == 128) ? (byte)255 : (byte)0 };
            var i32r = BitConverter.ToInt32(list.ToArray());

            var ft = low[0];//小数点位置

#if DEBUG
            System.Diagnostics.Debug.WriteLine(i32r);
#endif

            return i32r;
        }

        public enum DeviceType
        {
            ZNZS6E1R,
            HB965SF
        }



        public event Action<int> GotValue;


        public void Dispose()
        {
            isRun = false;
        }
    }
}
